﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Modules;

namespace DotNetNuke.Modules.UserDefinedTable
{


  public abstract partial class EditControl : Control
  {

    protected string customValidationMessage;
    protected string customValidationRule;

    //private string _DefaultValue;
    protected string editStyle;
    //private int _FieldID;
    //private string _FieldTitle;
    //private string _FieldType;
    //private string _HelpText;
    protected bool inputFilterScript;
    protected bool inputFilterTags;
    protected string inputSettings;
    protected InputType inputType;
    //protected ModuleInstanceContext modulecontext;
    //private int _ModuleId;
    //private bool _NormalizeFlag;
    //private string _OutputSettings;
    //private bool _Required;
    //private string _ValidationMessage;
    //private Control _ValueControl;







    public enum InputType
    {
      DropdownList,
      horizontalRadioButtons,
      verticalRadioButtons
    }




    protected EditControl()
    {
    }





    protected void AddListItems(ListControl control)
    {
      foreach (string str in this.InputSettings.Split(";".ToCharArray()))
      {
        ListItem item = new ListItem();
        if (str.Contains("|"))
        {
          string[] strArray = str.Split(new char[] { '|' });
          item.Text = strArray[0].Trim();
          item.Value = strArray[1].Trim();
        }
        else
          item.Value = str.Trim();
        
        control.Items.Add(item);
      }
    }





    protected static string ApplyScriptFilter(string Input)
    {
      Input = HttpUtility.HtmlEncode("<>" + Input);
      Input = new PortalSecurity().InputFilter(Input, PortalSecurity.FilterFlag.NoScripting);
      return HttpUtility.HtmlDecode(Input).Substring(2);
    }





    protected static string CleanID(string Name)
    {
      return XmlConvert.EncodeName(Name.Replace(" ", "_"));
    }





    public virtual void Initialise(
      string fieldTitle, string fieldType, int fieldId
      , int moduleId, string controlHelpText, string defaultValue
      , bool required, string validationRule, string validationMsg
      , string editStyle, string inputSettings, string outputSettings
      , bool normalizeFlag, bool inputFilterTags, bool inputFilterScript
      , InputType inputSettingsListType, ModuleInstanceContext moduleContext
    )
    {
      this.FieldTitle = fieldTitle;
      this.FieldType = fieldType;
      this.FieldID = fieldId;
      this.ModuleId = moduleId;
      this.HelpText = controlHelpText;
      this.DefaultValue = defaultValue;
      this.Required = required;
      this.customValidationRule = validationRule;
      this.customValidationMessage = validationMsg;
      this.InputSettings = inputSettings;
      this.OutputSettings = outputSettings;
      this.editStyle = editStyle;
      this.NormalizeFlag = normalizeFlag;
      this.inputFilterScript = inputFilterScript;
      this.inputFilterTags = inputFilterTags;
      this.inputType = inputSettingsListType;
      this.ModuleContext = moduleContext;
    }





    protected virtual bool IsNull()
    {
      return Null.IsNull(this.Value);
    }




    public bool IsValid()
    {
      if (this.Required && this.IsNull())
      {
        this.ValidationMessage = DotNetNuke.Services.Localization.Localization.GetString("Required2.ErrorMessage", LocalResourceFile);
        return false;
      }

      if ((UserDefinedTableController.GetMaxFieldSize() != Null.NullInteger) && (this.Value.Length >= UserDefinedTableController.GetMaxFieldSize()))
      {
        this.ValidationMessage = DotNetNuke.Services.Localization.Localization.GetString("ValueTooLong.ErrorMessage", LocalResourceFile);
        return false;
      }

      if (!this.IsValidType())
      {
        this.ValidationMessage = DotNetNuke.Services.Localization.Localization.GetString(string.Format("{0}.ErrorMessage", this.FieldType), LocalResourceFile);
        return false;
      }

      if (DataType.ByName(this.FieldType).SupportsValidation && !this.IsValidCustom())
      {
        this.ValidationMessage = this.customValidationMessage;
        return false;
      }

      return true;
    }



    /// <summary>
    /// this is original function renamed to _origName
    /// </summary>
    /// <returns></returns>
    private bool _isValidCustom()
    {
      if ( !string.IsNullOrEmpty(this.ValidationRule) && !string.IsNullOrEmpty(this.Value) )
        return Utilities.ValidateRegEx(this.Value, this.ValidationRule);

      return true;
    }





    protected virtual bool IsValidType()
    { return true; }





    public string DefaultValue
    { get; protected set; }




    public int FieldID
    { get; protected set; }





    public string FieldTitle
    { get; protected set; }




    public string FieldType
    { get; protected set; }




    public bool FilterScript
    { get; protected set; }





    public bool FilterTags
    { get; protected set; }




    public string HelpText
    { get; protected set; }




    public string InputSettings
    { get; protected set; }




    protected string[] InputValueList
    {
      get
      {
        return this.InputSettings.Split(';');
      }
    }




    protected bool IsNotAListOfValues
    {
      get
      {
        //System.Web.HttpContext.Current.Trace.Write("IsNotAListOfValues InputSettings" + Utilities.CStrN(this.InputSettings, "").Contains(";").ToString());
        return !Utilities.CStrN(this.InputSettings, "").Contains(";");
      }
    }




    public InputType ListInputType
    { get; protected set; }




    protected static string LocalResourceFile
    {
      get
      {
        return string.Format("~/{0}{1}/SharedResources.resx", Definition.PathOfModule, DotNetNuke.Services.Localization.Localization.LocalResourceDirectory);
      }
    }




    public ModuleInstanceContext ModuleContext
    { get; protected set; }




    public int ModuleId
    { get; protected set; }




    public bool NormalizeFlag
    { get; protected set; }




    public string OutputSettings
    { get; protected set; }




    protected static int PortalID
    { get { return PortalController.GetCurrentPortalSettings().PortalId; } }




    protected bool Required
    { get; set; }




    public string Style
    { get; protected set; }




    public string ValidationMessage
    { get; protected set; }




    public string ValidationRule
    { get; protected set; }




    public abstract string Value { get; set; }




    public Control ValueControl
    { get; set; }



  }

}

